package net.hangar5.xmlrpc;

/* CallParser.java

The contents of this file are subject to the Mozilla Public
License Version 1.1 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of
the License at http://www.mozilla.org/MPL/

Software distributed under the License is distributed on an "AS
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
implied. See the License for the specific language governing
rights and limitations under the License.

The Original Code is "Hangar5 XMLRPC Library".

The Initial Developer of the Original Code is James D. Rudnicki.  
Portions created by James D. Rudnicki are
Copyright (C) 2001.  All Rights Reserved.

Contributor(s): 
*/
import java.util.*;


/** Dissects XML-RPC call data into method name and parameters.
 */
public class CallParser extends XmlParser {

	static protected final int sizeMethNameO = 12;
	static protected final String tagMethNameC = "</methodName>";
	static protected final String tagMethNameO = "<methodName>";
	static protected final int sizeMethNameC = 13;

	static protected final String tagParamO = "<param>";
	static protected final String tagParamC = "</param>";
	static protected int sizeParamO = 7;
	static protected int sizeParamC = 8;
  
  

  protected int nIx1MethodName, nIx2MethodName;
  protected int nIxProcThru;
  
  private static final boolean debug =false;
  

  protected Vector vParams;
  
public CallParser(String s) {
	super(s);
	vParams = new Vector();
	return;
}
public CallParser(StringBuffer sb) {
	super(sb);
	vParams = new Vector();
	return;
}
  public String getMethodName() {
	return strSearch.substring( nIx1MethodName, nIx2MethodName );
  }  
  public Vector getParams() {
	return vParams;
  }  
/**
 * Parse between two indexes for a parameter.
 * @return java.lang.Object
 * @param ix1 int
 * @param ix2 int
 */
protected Object parseParam(int ixFocus1, int ixFocus2) throws RpcException {
	Object oRet = null;
	int i1, i2;
	
	if( debug ) {
		System.out.println( "param : "+ strSearch.substring( ixFocus1, ixFocus2 ));
	}

	/* find the outer <value> </value> tags */
  i1 = strSearch.indexOf( tagValueO, ixFocus1 );
  if( i1 < ixFocus2 ) {
	  i2 = strSearch.lastIndexOf( tagValueC, ixFocus2 );
	  if( i2 < 0 ) {
		  throw new RpcException( RpcException.PARSE, strSearch.substring( ixFocus1, ixFocus2 ) );
	  }
	  oRet = parseValue( i1+sizeValueO, i2 );
  }
  else {
	  throw new RpcException( RpcException.PARSE, strSearch.substring( ixFocus1, ixFocus2 ) );
  }
  
	return oRet;
}
public boolean parseXml() throws RpcException {
	int i, i1, i2;
	int nIxFocus1, nIxFocus2;
	nIxFocus1 = 0;

	/* pull off method name */
	i1 = strSearch.indexOf(tagMethNameO, nIxFocus1);
	if (i1 >= 0) {
		i2 = strSearch.indexOf(tagMethNameC, i1 + sizeMethNameO);
		nIx1MethodName = i1 + sizeMethNameO;
		nIx2MethodName = i2;
		if (debug) {
			String strTrace;
			strTrace = "method name: ";
			strTrace += strSearch.substring(nIx1MethodName, nIx2MethodName) + "\n\r";
			System.out.println(strTrace);
		}
		nIxFocus1 = nIx2MethodName + sizeMethNameC;
	}
	else {
		nIxFocus1 = 0;
		throw new RpcException(RpcException.GENERIC, RpcException.SGENERIC);
	}

	/* pull params (if any ) */
	do {
		i1 = strSearch.indexOf(tagParamO, nIxFocus1);
		if (i1 >= 0) {
			i2 = strSearch.indexOf(tagParamC, i1);
			Object o = parseParam(i1 + sizeParamO, i2);
			if (null != o) {
				vParams.addElement(o);
			}
			nIxFocus1 = i2 + sizeParamC;
		}
	}
	while (i1 >= 0);
	return true;
}
} // end of class
